package com.kd.juhaowan.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallbackProxy;
import com.hjq.toast.ToastUtils;
import com.kd.juhaowan.R;
import com.kd.juhaowan.http.api.GameGiftApi;
import com.kd.juhaowan.http.api.ReciveGiftApi;
import com.kd.juhaowan.http.model.GameGiftModel;
import com.kd.juhaowan.http.model.GameGiftResult;
import com.kd.juhaowan.http.model.GameModel;
import com.kd.juhaowan.http.model.GiftModel;
import com.kd.juhaowan.http.model.HttpData;
import com.kd.juhaowan.http.model.SmallAccountModel;
import com.kd.juhaowan.ui.adapter.BaseRecyclerViewAdapter;
import com.kd.juhaowan.ui.holder.GameGiftViewHolder;
import com.kd.juhaowan.ui.listener.OnGiftReciveListener;
import com.kd.juhaowan.utils.DeviceUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import butterknife.BindView;
import butterknife.OnClick;

public class GameGiftActivity extends BaseRefreshActivity implements OnGiftReciveListener {

    @BindView(R.id.view_refreshlist_refresh)
    SmartRefreshLayout refreshLayout;





    @BindView(R.id.gift_listview)
    RecyclerView recyclerView;

    private BaseRecyclerViewAdapter<GameGiftViewHolder, GameGiftModel> giftAdapter;
    @BindView(R.id.activity_title)
    TextView titleView;
    @BindView(R.id.activity_more)
    TextView moreView;
    @BindView(R.id.gamedjq_tips)
    TextView tipView;
    @BindView(R.id.gamedjq_tips_setting)
    TextView tipSettingView;


    @Override
    protected SmartRefreshLayout getSmartRefreshLayout() {
        return refreshLayout;
    }

    @Override
    protected String getSmartRefreshTaskTag() {
        return null;
    }

    SmallAccountModel accountModel ;

    @OnClick(R.id.gamedjq_tips_setting)
    public void onSettingClick(){
        Intent intent=new Intent(this,SmallAccountActivity.class);
        intent.putExtra("game_id",gameModel.getId());
        if(status==2){
            intent.putExtra("account_id",accountModel.getTrumpet_user_id());
        }
        launcher.launch(intent);
    }
    ActivityResultLauncher<Intent> launcher;
    int status=0;
    @Override
    protected void onRequestNextPageData(int page) {
        EasyHttp.post(this).api(new GameGiftApi(gameModel.getId(),page)).request(new HttpCallbackProxy<HttpData<GameGiftResult>>(this){
            @Override
            public void onHttpSuccess(HttpData<GameGiftResult> result, boolean cache) {
                super.onHttpSuccess(result, cache);
                status = result.getData().getSmall_account_status();
                accountModel = result.getData().getSmall_account();
                if(status==0){
                    tipView.setText("请先创建游戏角色再领取~");
                }else if (status==1){
                    tipView.setText("请设置默认领取角色~");
                    tipSettingView.setText("设置>");
                }else if(accountModel!=null){
                    tipView.setText((TextUtils.isEmpty(accountModel.getNickname())?accountModel.getUsername():accountModel.getNickname())+" - "+accountModel.getServer_name() +" - "+accountModel.getRole_name());
                    tipSettingView.setText("切换>");
                }
                if (page==1){

                    giftAdapter.resetAdapterData(result.getData().getList());
                }else {
//                    giftAdapter.appendAdapterData(result.getData().getList());
                }

            }
        });
    }



//    private List<Object> getSourceList(GameGiftResult result){
//        List<GameGiftModel> models = result.getList();
//        List<Object> objects = new ArrayList<>();
//        if(models!=null&&models.size()>0){
//            for (GameGiftModel temp:models) {
//                objects.add(temp.getName());
//                objects.addAll(temp.getGiftpackList());
//            }
//        }
//        return  objects;
//    }


//    @Override
//    public void onItemClick(RecyclerView.ViewHolder viewHolder, View view, int position) {
//
//    }
//
//    @Override
//    public void onItemDianzanClick(BaseViewHolder viewHolder, int position) {
////        GiftModel model = (GiftModel)giftAdapter.getItem(position);
////        EasyHttp.post(this).api(new ReciveGiftApi(model.getId(),gameModel.getId(), DeviceUtil.isInstallApp(this,gameModel.getGame_package_name())?1:2)).request(new HttpCallbackProxy<HttpData<GiftModel>>(this){
////            @Override
////            public void onHttpSuccess(HttpData<GiftModel> result, boolean cache) {
////                super.onHttpSuccess(result, cache);
////                model.setIs_receive(1);
//////                    model.setCode(result.getMessage());
////                giftAdapter.notifyItemChanged(position,model);
////                ToastUtils.show("领取成功");
////            }
////        });
//    }

    @Override
    public void onRecive(GiftModel model) {
        EasyHttp.post(this).api(new ReciveGiftApi(model.getId(),gameModel.getId(), DeviceUtil.isInstallApp(this,gameModel.getGame_package_name())?1:2)).request(new HttpCallbackProxy<HttpData<GiftModel>>(this){
            @Override
            public void onHttpSuccess(HttpData<GiftModel> result, boolean cache) {
                super.onHttpSuccess(result, cache);
                onRefresh(getSmartRefreshLayout());
                ToastUtils.show("领取成功");
            }
        });
    }

    @Override
    protected boolean isNullData(HttpData response) {
        return false;
    }
    private GameModel gameModel;

    @Override
    protected void reInitDate() {
        onRefresh(getSmartRefreshLayout());
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        gameModel = (GameModel)getIntent().getSerializableExtra("game");
//        gameid = getIntent().getStringExtra("game_id");
//        gameName = getIntent().getStringExtra("game_name");
        titleView.setText(gameModel.getName()+"-礼包");
        moreView.setText("我的礼包");
        initAdapter();
        onRefresh(getSmartRefreshLayout());
        getSmartRefreshLayout().setEnableLoadMore(false);
        getSmartRefreshLayout().setEnableRefresh(false);
//        setContentView(R.layout.activity_account_login);
        launcher =registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
                new ActivityResultCallback<ActivityResult>() {
                    @Override
                    public void onActivityResult(ActivityResult result) {
                        int resultCode = result.getResultCode();
                        if (resultCode == RESULT_OK) {
                            status =2;
                            accountModel= (SmallAccountModel)result.getData().getSerializableExtra("model");
                            tipView.setText(accountModel.getNickname()+" - "+accountModel.getServer_name() +" - "+accountModel.getRole_name());
                            tipSettingView.setText("切换>");
                        } else if (resultCode == RESULT_CANCELED) {
                            Log.i("adb", "onActivityResult PictureSelector Cancel");
                        }
                    }
                });
    }
    @OnClick(R.id.actvity_back)
    public void onBackClick(){
        finish();
    }

    @OnClick(R.id.activity_more)
    public void onMoreClick(){
        startActivity(new Intent(GameGiftActivity.this,MineGiftActivity.class));
    }
    @Override
    protected int getViewLayoutResourceId() {
        return R.layout.activity_game_gift;
    }

    private void initAdapter(){
        giftAdapter = new BaseRecyclerViewAdapter<GameGiftViewHolder, GameGiftModel>() {
            @NonNull
            @Override
            public GameGiftViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                LayoutInflater inflater = LayoutInflater.from(parent.getContext());

                    View view = inflater.inflate(R.layout.item_game_gift, null, false);
                GameGiftViewHolder holder=new GameGiftViewHolder(view,GameGiftActivity.this);

                holder.setOnItemClickListener(getOnItemClickListener());
                return  holder;

            }

            @Override
            public void onBindViewHolder(@NonNull GameGiftViewHolder holder, int position) {
                holder.setDate(getItem(position));



            }

            @Override
            public int getItemViewType(int position) {
                Object obj = getItem(position);
                if(obj instanceof GiftModel){
                    return 2;
                }else {
                    return 1;
                }
            }
        };
        giftAdapter.setOnItemClickListener(null);
        giftAdapter.resetAdapterData(null);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(RecyclerView.VERTICAL);

        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(giftAdapter);



    }


}
